package com.ruoyi.peach.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.peach.domain.RecordImg;
import com.ruoyi.peach.mapper.RecordImgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.peach.mapper.CollectRecordMapper;
import com.ruoyi.peach.domain.CollectRecord;
import com.ruoyi.peach.service.ICollectRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采集记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-22
 */
@Service
public class CollectRecordServiceImpl implements ICollectRecordService 
{
    @Autowired
    private CollectRecordMapper collectRecordMapper;
    @Autowired
    private RecordImgMapper recordImgMapper;

    /**
     * 查询采集记录
     * 
     * @param recordId 采集记录主键
     * @return 采集记录
     */
    @Override
    public CollectRecord selectCollectRecordByRecordId(Long recordId)
    {
        return collectRecordMapper.selectCollectRecordByRecordId(recordId);
    }

    /**
     * 查询采集记录列表
     * 
     * @param collectRecord 采集记录
     * @return 采集记录
     */
    @Override
    public List<CollectRecord> selectCollectRecordList(CollectRecord collectRecord)
    {
        return collectRecordMapper.selectCollectRecordList(collectRecord);
    }

    /**
     * 新增采集记录
     * 
     * @param collectRecord 采集记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCollectRecord(CollectRecord collectRecord)
    {
        collectRecord.setCreateTime(DateUtils.getNowDate());

        int rows = collectRecordMapper.insertCollectRecord(collectRecord);
        Long recordId = collectRecord.getRecordId();
        if (rows == 1) {
            String imageUrls = collectRecord.getImageUrls();
            if (imageUrls != null && !imageUrls.trim().isEmpty()) {
                RecordImg recordImg = new RecordImg();
                recordImg.setRecordId(recordId);
                recordImg.setImgUrls(imageUrls);
                recordImg.setUploadTime(collectRecord.getCollectTime());
                System.out.println(recordImg);
                recordImgMapper.insertRecordImg(recordImg);
            }

            // ✅ 成功返回详细信息
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 修改采集记录
     * 
     * @param collectRecord 采集记录
     * @return 结果
     */
    @Override
    public int updateCollectRecord(CollectRecord collectRecord)
    {
        return collectRecordMapper.updateCollectRecord(collectRecord);
    }

    /**
     * 批量删除采集记录
     * 
     * @param recordIds 需要删除的采集记录主键
     * @return 结果
     */
    @Override
    public int deleteCollectRecordByRecordIds(Long[] recordIds)
    {
        return collectRecordMapper.deleteCollectRecordByRecordIds(recordIds);
    }

    /**
     * 删除采集记录信息
     * 
     * @param recordId 采集记录主键
     * @return 结果
     */
    @Override
    public int deleteCollectRecordByRecordId(Long recordId)
    {
        return collectRecordMapper.deleteCollectRecordByRecordId(recordId);
    }

    /**
     * 根据ID列表查询采集记录
     *
     * @param recordIds 记录ID数组
     * @return 采集记录
     */
    @Override
    public List<CollectRecord> selectCollectRecordListByIds(Long[] recordIds) {
        return collectRecordMapper.selectCollectRecordListByIds(recordIds);
    }

}
